package com.hz.monitoring.module.hardware.controller.admin.mcu_tenant_type;

import com.hz.monitoring.framework.common.pojo.CommonResult;
import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.excel.core.util.ExcelUtils;
import com.hz.monitoring.framework.operatelog.core.annotations.OperateLog;
import com.hz.monitoring.module.hardware.controller.admin.mcu_tenant_type.vo.*;
import com.hz.monitoring.module.hardware.convert.mcu_tenant_ype.McuTenantTypeConvert;
import com.hz.monitoring.module.hardware.dal.dataobject.mcu_tenant_type.McuTenantTypeDO;
import com.hz.monitoring.module.hardware.service.mcu_tenant_type.McuTenantTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.hz.monitoring.framework.common.pojo.CommonResult.success;
import static com.hz.monitoring.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - MCU租户分类")
@RestController
@RequestMapping("/hardware/mcu-tenant-type")
@Validated
public class McuTenantTypeController {

    @Resource
    private McuTenantTypeService mcuTenantTypeService;

    @PostMapping("/create")
    @ApiOperation("创建MCU租户分类")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-type:create')")
    public CommonResult<Long> createMcuTenantType(@Valid @RequestBody McuTenantTypeCreateReqVO createReqVO) {
        return success(mcuTenantTypeService.createMcuTenantType(createReqVO));
    }

    @PostMapping("/create-batch")
    @ApiOperation("批量创建MCU租户分类")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-type:create')")
    public CommonResult<Boolean> createBatchMcuTenantType(@Valid @RequestBody List<McuTenantTypeCreateReqVO> createReqVOList) {
        return success(mcuTenantTypeService.createBatchMcuTenantType(createReqVOList));
    }

    @PutMapping("/update")
    @ApiOperation("更新MCU租户分类")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-type:update')")
    public CommonResult<Boolean> updateMcuTenantType(@Valid @RequestBody McuTenantTypeUpdateReqVO updateReqVO) {
        mcuTenantTypeService.updateMcuTenantType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除MCU租户分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-type:delete')")
    public CommonResult<Boolean> deleteMcuTenantType(@RequestParam("id") Long id) {
        mcuTenantTypeService.deleteMcuTenantType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得MCU租户分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-type:query')")
    public CommonResult<McuTenantTypeRespVO> getMcuTenantType(@RequestParam("id") Long id) {
        McuTenantTypeDO mcuTenantType = mcuTenantTypeService.getMcuTenantType(id);
        return success(McuTenantTypeConvert.INSTANCE.convert(mcuTenantType));
    }

    @GetMapping("/list")
    @ApiOperation("获得MCU租户分类列表")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-type:query')")
    public CommonResult<List<McuTenantTypeRespVO>> getMcuTenantTypeList(McuTenantTypeListReqVO listReqVO) {
        List<McuTenantTypeDO> list = mcuTenantTypeService.getMcuTenantTypeList(listReqVO);
        return success(McuTenantTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得MCU租户分类分页")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-type:query')")
    public CommonResult<PageResult<McuTenantTypeRespVO>> getMcuTenantTypePage(@Valid McuTenantTypePageReqVO pageVO) {
        PageResult<McuTenantTypeDO> pageResult = mcuTenantTypeService.getMcuTenantTypePage(pageVO);
        return success(McuTenantTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出MCU租户分类 Excel")
    @PreAuthorize("@ss.hasPermission('hardware:mcu-tenant-type:export')")
    @OperateLog(type = EXPORT)
    public void exportMcuTenantTypeExcel(@Valid McuTenantTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<McuTenantTypeDO> list = mcuTenantTypeService.getMcuTenantTypeList(exportReqVO);
        // 导出 Excel
        List<McuTenantTypeExcelVO> datas = McuTenantTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "MCU租户分类.xls", "数据", McuTenantTypeExcelVO.class, datas);
    }

}
